*********************************************************************************
* Title: Analyses																*
* Paper: Why is the AfD so successful in East Germany?							*
* Authors: M. Pesthy, M. Mader, H. Schoen										*
*********************************************************************************

*********************************************************************************
* Figure 1																		*
*********************************************************************************
	
	*Populism
	use DATA_WKP17.dta, clear
	catplot ost pop_g_r2 [aw=weight], percent(ost) recast(bar) asyvars ylabel(, ang(h)) bar(1, bcolor(gs4)) bar(2, bcolor(gs12)) ///
	yscale(range(0 50)) ylabel(0 (10) 60) ytitle(" ", size(vhuge)) ///
	scheme(plotplainblind) title("Populism", size(vhuge)) ylabel(,labsize(huge)) ///
	name(g1, replace) legend(off) ///
	graphregion(margin(-10 0 0 0)) var2opts(relabel(1 "low" 2 "2" 3 "3" 4 "4" 5 "high") label(labsize(*2.75)))  ///
	xsize(3) ysize(2)
	
	tab pop_g pop_g_r2, mis

	*Nativism
	use DATA_WKP17.dta, clear
	catplot ost nat_g1_r2 [aw=weight], percent(ost) recast(bar) asyvars ylabel(, ang(h)) bar(1, bcolor(gs4)) bar(2, bcolor(gs12)) ///
	yscale(range(0 50)) ylabel(0 (10) 60) ytitle(" ", size(vhuge)) ///
	scheme(plotplainblind) title("Nativism", size(vhuge)) ylabel(,labsize(huge)) ///
	name(g2, replace) legend(size(large)  title(" ") position(2)  ring(0)) ///
	graphregion(margin(-10 0 0 0)) var2opts(relabel(1 "low" 2 "2" 3 "3" 4 "4" 5 "high") label(labsize(*2.75)))  ///
	xsize(3) ysize(2)
	

	graph combine g1 g2, iscale(1) ysize(4) xsize(10) graphregion(margin(0 0 0 0)) 
	graph export Figure1.eps, replace
	graph export Figure1.tif, replace
	graph export Figure1.svg, replace
	graph export Figure1.pdf, replace
	
			*TEXT:
			use DATA_WKP17.dta, clear
			recode pop_g_r2 (1 2 3 = 0) (4 5 = 1)
			recode nat_g1_r2 (1 2 3 = 0) (4 5 = 1)
	
			bysort ost: ci means pop_g_r2 [aw = weight]

			bysort ost: ci means nat_g1_r2 [aw = weight]
			
*********************************************************************************
* Figure 2 																		*
*********************************************************************************
			
	*Populism: Homogeneity
	use DATA_WKP17.dta, clear
	catplot ost pop_hom_r2 [aw=weight], percent(ost) recast(bar) asyvars ylabel(, ang(h)) bar(1, bcolor(gs4)) bar(2, bcolor(gs12)) ///
	yscale(range(0 50)) ylabel(0 (10) 60) ytitle(" ", size(large)) ///
	scheme(plotplainblind) title("Homogeneity", size(huge)) ylabel(,labsize(huge)) ///
	name(g3, replace) legend(off) ///
	graphregion(margin(-10 0 0 0)) var2opts(relabel(1 "low" 2 "2" 3 "3" 4 "4" 5 "high") label(labsize(*2)))  ///
	xsize(3) ysize(2)

	*Populism: Anti-elite sentiment
	use DATA_WKP17.dta, clear
	catplot ost pop_eli_r2 [aw=weight], percent(ost) recast(bar) asyvars ylabel(, ang(h)) bar(1, bcolor(gs4)) bar(2, bcolor(gs12)) ///
	yscale(range(0 50)) ylabel(0 (10) 60) ytitle(" ", size(large)) ///
	scheme(plotplainblind) title("Anti-Elite", size(huge)) ylabel(,labsize(huge)) ///
	name(g4, replace) legend(off) ///
	graphregion(margin(-10 0 0 0)) var2opts(relabel(1 "low" 2 "2" 3 "3" 4 "4" 5 "high") label(labsize(*2)))  ///
	xsize(3) ysize(2)
	
	*Populism: Sovereignty
	use DATA_WKP17.dta, clear
	catplot ost pop_sov_r2 [aw=weight], percent(ost) recast(bar) asyvars ylabel(, ang(h)) bar(1, bcolor(gs4)) bar(2, bcolor(gs12)) ///
	yscale(range(0 50)) ylabel(0 (10) 60) ytitle(" ", size(large)) ///
	scheme(plotplainblind) title("Sovereignty", size(huge)) ylabel(,labsize(huge)) ///
	name(g5, replace) legend(size(large)  title(" ") position(2)  ring(0)) ///
	graphregion(margin(-10 0 0 0)) var2opts(relabel(1 "low" 2 "2" 3 "3" 4 "4" 5 "high") label(labsize(*2)))  ///
	xsize(3) ysize(2)
	
	graph combine g3 g4 g5, col(3) title("Populism", position(11) size(huge)) scheme(plotplainblind) name(g345, replace)
	
			*TEXT:
			use DATA_WKP17.dta, clear
			fre pop_sov_r2
			recode pop_sov_r2 (1 2 3 = 0) (4 5 = 1)

			bysort ost: ci means pop_sov_r2 [aw = weight]
	
	
	*Nativism: Ethnonationalism
	use DATA_WKP17.dta, clear
	catplot ost ethn1_r2 [aw=weight], percent(ost) recast(bar) asyvars ylabel(, ang(h)) bar(1, bcolor(gs4)) bar(2, bcolor(gs12)) ///
	yscale(range(0 50)) ylabel(0 (10) 60) ytitle(" ", size(large)) ///
	scheme(plotplainblind) title("Ethnonationalism", size(huge)) ylabel(,labsize(huge)) ///
	name(g6, replace) legend(off) ///
	graphregion(margin(-10 0 0 0)) var2opts(relabel(1 "low" 2 "2" 3 "3" 4 "4" 5 "high") label(labsize(*2)))  ///
	xsize(3) ysize(2)

	
	*Nativism: Xenophobia
	use DATA_WKP17.dta, clear
	catplot ost xeno1_r2 [aw=weight], percent(ost) recast(bar) asyvars ylabel(, ang(h)) bar(1, bcolor(gs4)) bar(2, bcolor(gs12)) ///
	yscale(range(0 50)) ylabel(0 (10) 60) ytitle(" ", size(large)) ///
	scheme(plotplainblind) title("Xenophobia", size(huge)) ylabel(,labsize(huge)) ///
	name(g7, replace) legend(size(large)  title(" ") position(2)  ring(0)) ///
	graphregion(margin(-10 0 0 0)) var2opts(relabel(1 "low" 2 "2" 3 "3" 4 "4" 5 "high") label(labsize(*2)))  ///
	xsize(3) ysize(2)
	
	
	graph combine g6 g7, title("Nativism", position(11) size(huge)) scheme(plotplainblind) name(g67, replace) graphr(m(r+55)) 
	
	graph combine g345 g67, col(1) scheme(plotplainblind)
	
	graph export Figure2.eps, replace

	
*********************************************************************************
* Table 1: Populism and nativism in East and West Germany						*
*********************************************************************************
	use DATA_WKP17.dta, clear
	
	recode pop_g_r2 nat_g1_r2 pop_hom_r2 pop_eli_r2 pop_sov_r2 ethn1_r2 xeno1_r2 (1 2 3 = 0) (4 5 = 1) 

	*Column 1:
	bysort ost: sum pop_g_r2 nat_g1_r2 pop_hom_r2 pop_eli_r2 pop_sov_r2 ethn1_r2 xeno1_r2 [aw=weight]
	
	*Column 2/3:
	bysort ost: sum pop_g_r2 nat_g1_r2 pop_hom_r2 pop_eli_r2 pop_sov_r2 ethn1_r2 xeno1_r2 if age < 50 [aw=weight]
	bysort ost: sum pop_g_r2 nat_g1_r2 pop_hom_r2 pop_eli_r2 pop_sov_r2 ethn1_r2 xeno1_r2 if age >= 50 [aw=weight]
	

	
*** FOOTNOTE 5 about interaction effects of age and region
	reg pop_g_r2 i.age_cat##i.ost [aw=weight]
	reg nat_g1_r2 i.age_cat##i.ost [aw=weight]
	reg pop_hom_r2 i.age_cat##i.ost [aw=weight]	
	reg pop_eli_r2 i.age_cat##i.ost [aw=weight]	
	reg pop_sov_r2 i.age_cat##i.ost [aw=weight]	
	reg ethn1_r2 i.age_cat##i.ost [aw=weight]	
	reg xeno1_r2 i.age_cat##i.ost [aw=weight]


*********************************************************************************
* Table 2: Logistic regressions East/West										*
*********************************************************************************

	use DATA_WKP17.dta, clear

	gen filter = .
	replace filter = 1 if wafd8 != . & pop_g_r != . & nat_g1_r != . & female != . & educat != . & age != . & w1_pint_r != . & ewl1 != . & rural2 != . & ost != . 

	* Model with pop_g
		eststo m1w: quietly logit wafd8 pop_g_r female i.educat age w1_pint_r ewl1 i.rural2 if ost == 0 & filter == 1 [pw=weight]
		eststo m1o: quietly logit wafd8 pop_g_r female i.educat age w1_pint_r ewl1 i.rural2 if ost == 1 & filter == 1  [pw=weight]
		
	* Model with nat_g1
		eststo m2w: quietly logit wafd8 nat_g1_r female i.educat age w1_pint_r ewl1 i.rural2 if ost == 0 & filter == 1  [pw=weight]
		eststo m2o: quietly logit wafd8 nat_g1_r female i.educat age w1_pint_r ewl1 i.rural2 if ost == 1 & filter == 1  [pw=weight]	
		
	* Model with both pop_g_r and nat_g1
		eststo m3w: quietly logit wafd8 pop_g_r nat_g1_r female i.educat age w1_pint_r ewl1 i.rural2 if ost == 0 & filter == 1  [pw=weight]	
		eststo m3o: quietly logit wafd8 pop_g_r nat_g1_r female i.educat age w1_pint_r ewl1 i.rural2 if ost == 1 & filter == 1  [pw=weight]
	
	* Table m1-m3	
		esttab 	m1w m1o m2w m2o m3w m3o using "Table_2.rtf", replace ///
				b(2) se(2) nogaps compress nobaselevels varwidth(15) ///
				modelwidth(6) nonumbers ///
				mtitles("M1: West" "M1: East" "M2: West" "M2: East" "M3: West" "M3: East") ///
				coeflabels(	iafd1 "PID AfD" ///
							female "Female" ///
							2.educat "Education: middle" ///
							3.educat "Education: high" ///
							age "Age in years" ///
							w1_pint_r "Political Interest" ///
							ewl1 "Personal Economic Situation" ///
							1.rural2 "Rural Environment" ///
							pop_g_r "Populism" ///
							nat_g1_r "Nativism" ///
							_cons "Constant") ///
				order(pop_g_r nat_g1_r)


*********************************************************************************************************
* Figure 3: Average Marginal Effects of Populism and Nativism on AfD-Support in East and West Germany	*
*********************************************************************************************************

		estimates restore m1w
		margins, dydx(pop_g) post // cf. text
		eststo marg1w
		
		estimates restore m1o
		margins, dydx(pop_g) post // cf. text
		eststo marg1o
		
		estimates restore m2w
		margins, dydx(nat_g1) post
		eststo marg2w
		
		estimates restore m2o
		margins, dydx(nat_g1) post 
		eststo marg2o
		
		estimates restore m3w
		margins, dydx(pop_g_r nat_g1_r) post // cf. text
		eststo marg3w
		
		estimates restore m3o
		margins, dydx(pop_g_r nat_g1_r) post // cf. text
		eststo marg3o

	set scheme s1mono
	coefplot (marg1w, mcolor(black) msize(vlarge) msymbol(circle) ciopts(lcolor(black) lwidth(thick)))  ///
			 (marg1o, mcolor(gs10) msize(vlarge) msymbol(diamond) ciopts(lcolor(gs10) lwidth(thick))) ///
			 || (marg2w, mcolor(black) msize(vlarge) msymbol(circle) ciopts(lcolor(black) lwidth(thick))) ///
			 (marg2o, mcolor(gs10) msize(vlarge) msymbol(diamond) ciopts(lcolor(gs10) lwidth(thick))) ///
			 || (marg3w, mcolor(black) msize(vlarge) msymbol(circle) ciopts(lcolor(black) lwidth(thick))) ///
			 (marg3o,  mcolor(gs10) msize(vlarge) msymbol(diamond) ciopts(lcolor(gs10) lwidth(thick))) ///
			 ||, byopts(cols(3)legend(off)) norecycle ///
		bylabels("Model 1" "Model 2" "Model 3") subtitle(,bcolor(none) size(huge)) ///
		coeflabels(pop_g_r="Populism" nat_g1_r="Nativism", labsize (huge))  xlab(-.1 (.1) .6, labs(huge)) ///
		graphregion(ifcolor(white)) xline(0, lpattern(dash) lcolor(gs8)) ///
		xtitle("Average marginal effect", size(huge)) /// 
		legend(off) text(.7 .02 "West", size(huge) placement(e)) text(1 .1 "East", color(gs10) size(huge) placement(e)) ///	EDIT MARKER LABELS IN GRAPH EDITOR
		xsize(3) ysize(1) name("Figure3", replace)
		
		graph export Figure3.eps, replace

			
*********************************************************************************
* Table 3 & Figure 4: The Joint effect of populist and nativist attitudes on AfD support	*
*********************************************************************************

	use DATA_WKP17.dta, clear

	logit wafd8 ib3.type2 i.female i.educat c.age c.w1_pint_r c.ewl1 i.rural2 if ost == 0 [pw=weight]
	margins
	margins, at(type2 = (1 2 3 4)) post // Table 3
	eststo marg1

	logit wafd8 ib3.type2 i.female i.educat c.age c.w1_pint_r c.ewl1 i.rural2 if ost == 1 [pw=weight]
	margins
	margins, at(type2 = (1 2 3 4)) post // Table 3
	eststo marg2
	
	fre wafd8 if ost == 0 [aw=weight]
	fre wafd8 if ost == 1 [aw=weight]

	coefplot (marg1, mcolor(black) msize(large) msymbol(circle) ciopts(lcolor(black) lwidth(thick))) ///
			 (marg2, mcolor(gs10) msize(large) msymbol(diamond) ciopts(lcolor(gs10) lwidth(thick))) , ///
		scheme(s1mono) ///
		coeflabels(	1._at="Group 1: low populism, low nativism" ///
					2._at = "Group 2: high populism, low nativism" ///
					3._at = "Group 3: low populism, high nativism" ///
					4._at = "Group 4: high populism, high nativism", labsize (vlarge)) ///
		xtitle("Probability of AfD vote", size(vlarge)) xlab(0 (.1) .4, labs(vlarge)) ///
		legend(off) text(.8 .08 "West", size(large) placement(e)) text(1.125 .13 "East", color(gs10) size(large) placement(e)) ///
		subtitle(,bcolor(none)) graphregion(ifcolor(white)) ///
		xline(.13, lpattern(dash) lcolor(gs12)) text(4.1 .125  "Prop.", size(large) color(gs12)  placement(w)) text(4.3 .125  "West", size(large) color(gs12) placement(w)) ///
		xline(.20, lpattern(dash) lcolor(gs12)) text(4.1 .195  "Prop.", size(large) color(gs12)  placement(w)) text(4.3 .195  "East", size(large) color(gs12) placement(w)) ///
		xsize(2) ysize(1) name(Figure4, replace)	
	
	graph export Figure4.eps, replace
